package com.lucy.disk.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.lucy.disk.constant.FileConst;
import com.lucy.disk.constant.FileTypeEnum;
import com.lucy.disk.domain.entity.FileEntity;
import com.lucy.disk.domain.model.BaseFile;
import com.lucy.disk.domain.model.ImageModel;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author kindear
 * 网盘工具类
 */
public class DiskUtil {


    /**
     * 计算文件类型 -- 不再作为直接方法使用
     * @param fileName
     * @return
     */
    @Deprecated
    public static FileTypeEnum calcFileType(String fileName){
        String extName = FileUtil.extName(fileName);
        if (FileConst.APP_FILE.contains(extName)){
            return FileTypeEnum.OTHER;
        }
        else if (FileConst.DOC_FILE.contains(extName)){
            return FileTypeEnum.DOCUMENT;
        }
        else if (FileConst.IMG_FILE.contains(extName)){
            return FileTypeEnum.PICTURE;
        }
        else if (FileConst.VIDEO_FILE.contains(extName)){
            return FileTypeEnum.VIDEO;
        }else {
            return FileTypeEnum.OTHER;
        }
    }

    /**
     * 获取文件类型
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName){
        FileTypeEnum fileTypeEnum = calcFileType(fileName);
        return fileTypeEnum.getDesc();
    }

    /**
     * 是否允许创建判断 -- 目前仅允许创建文档
     * @param fileName
     * @return
     */
    public static boolean isDoc(String fileName){
        String extName = FileUtil.extName(fileName);
        // 后缀名为空默认创建目录
        if (Validator.isEmpty(extName)){
            return true;
        }
        if (FileConst.DOC_FILE.contains(extName)){
            return true;
        }
        return false;
    }

    /**
     * 构建选择树 原始方法
     * @param files
     * @param rootId
     * @return
     */
    public static List<Tree<String>> fileTreeBuild(List<FileEntity> files, String rootId){
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("isDir");
        treeNodeConfig.setParentIdKey("dirId");
        return TreeUtil.build(files, rootId, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getDirId());
                    tree.setWeight(treeNode.getIsDir());
                    tree.setName(treeNode.getFileName());
                    // 是否为文件夹
                    tree.putExtra("isDir",treeNode.getIsDir());
                });
    }

    /**
     * 获取全部后代节点
     * @param nodeList
     * @return
     */
    public static List<String> getAllChild(List<Tree<String>> nodeList){
        if (Validator.isEmpty(nodeList)){
            return new ArrayList<>();
        }
        Set<String> resSet = new HashSet<>();
        nodeList.forEach(stringTree -> {
            resSet.add(stringTree.getId());
            if (Validator.isNotEmpty(stringTree.getChildren()) && stringTree.getChildren().size() != 0){
                resSet.addAll(getAllChild(stringTree.getChildren()));
            }
        });
        return new ArrayList<>(resSet);
    }

}
